#include "musicplayer.h"
#include "ui_musicplayer.h"


MusicPlayer::MusicPlayer(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MusicPlayer)//注意：此处的类型要与ui中的对象名一致，否则可能会提醒未识别
{
    ui->setupUi(this);
    //设置运行窗口大小
    setGeometry(900,300,1200,800);
    initUi();//初始化各种信息

    connectSignalAndSlots();//记得先把信号先关联上
}

MusicPlayer::~MusicPlayer()
{
    delete ui;
}

//处理文本和图片的路径信息
QJsonArray MusicPlayer::randomPiction()
{
    // 推荐文本 + 推荐图片路径
    QVector<QString> vecImageName;
    vecImageName<<"001.png"<<"003.png"<<"004.png"<<"005.png"<<"006.png"<<"007.png"
                 <<"008.png"<<"009.png"<<"010.png"<<"011.png"<<"012.png"<<"013.png"
                 <<"014.png"<<"015.png"<<"016.png"<<"017.png"<<"018.png"<<"019.png"
                 <<"020.png"<<"021.png"<<"022.png"<<"023.png"<<"024.png"<<"025.png"
                 <<"026.png"<<"027.png"<<"028.png"<<"029.png"<<"030.png"<<"031.png"
                 <<"032.png"<<"033.png"<<"034.png"<<"035.png"<<"036.png"<<"037.png"
                 <<"038.png"<<"039.png"<<"040.png";

    //打乱上述顺序
    std::random_shuffle(vecImageName.begin(), vecImageName.end());//

    // 001.png
    // path: ":/images/rec/"+vecImageName[i];
    // text: "推荐-001"
    QJsonArray objArray;
    for(int i = 0; i < vecImageName.size(); ++i)
    {
        QJsonObject obj;
        obj.insert("path", ":/images/rec/"+vecImageName[i]);//打乱了顺序，所以此时的图片00x就不一定对得上i了；

        QString strText = QString("推荐-%1").arg(i, 3, 10, QChar('0'));//text的推荐数为i
        obj.insert("text", strText);

        objArray.append(obj);

    }

    return objArray;

}

void MusicPlayer::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons() == Qt::LeftButton )// QPoint  dragPos;

    {
        move(event->globalPos() - dragPos);

        //qDebug()<<"mouse move";
        return;
    }

    QWidget::mouseMoveEvent(event);
}

void MusicPlayer::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        //isDrag = true;
        // 获取鼠标相对于电脑屏幕左上角的全局坐标
        dragPos = event->globalPos() - geometry().topLeft();
        return;
    }

    QWidget::mousePressEvent(event);
}

void MusicPlayer::initUi()
{
    //给窗口设置阴影效果时，需要将窗口标题栏无边框，背景设置为透明
    setWindowFlag(Qt::WindowType::FramelessWindowHint);
    // 设置窗口背景透明
    this->setAttribute(Qt::WA_TranslucentBackground);
    // 给窗口设置阴影效果
    QGraphicsDropShadowEffect* shadowEffect = new QGraphicsDropShadowEffect(this);
    shadowEffect->setOffset(0,0); // 设置阴影偏移
    shadowEffect->setColor("#000000"); // 设置阴影颜色：黑色
    shadowEffect->setBlurRadius(20); // 设置阴影的模糊半径
    this->setGraphicsEffect(shadowEffect);


    //bodyLeft
    //bodyleft中我的音乐和在线音乐的按钮是自定义按键，需要自己设计，提升为，此后要美化该按键只能通过代码的方式
    //设置btform的图标
    ui->rec->setIconAndText(":/images/rec.png", "推荐", 0);
    ui->radio->setIconAndText(":/images/radio.png", "电台", 1);
    ui->musicHall->setIconAndText(":/images/music.png", "音乐馆", 2);
    ui->like->setIconAndText(":/images/like.png", "我喜欢", 3);
    ui->local->setIconAndText(":/images/local.png", "本地下载", 4);
    ui->recent->setIconAndText(":/images/recent.png", "最近播放", 5);

    ui->local->showAnimal();
    ui->stackedWidget->setCurrentIndex(4);
    //currentPage = ui->localPage;

    //bodyRight：层叠区
    //推荐页面也是自定义按键，即recbox；而recbox中的组件也是自定义组件，为recboxitem；
    // 初始化推荐页面
    srand(time(NULL));
    //初始化推荐区和音乐补给区：一行与两行
    ui->recMusicBox->initRecBoxUi(randomPiction(), 1);
    ui->supplyMusicBox->initRecBoxUi(randomPiction(), 2);

    //添加commonpage页的信息
    ui->likePage->setCommonPageUI("我喜欢", ":/images/ilikebg.png");
    ui->localPage->setCommonPageUI("本地音乐", ":/images/localbg.png");
    ui->recentPage->setCommonPageUI("最近播放", ":/images/recentbg.png");

    // 创建音量调节窗口对象并挂到对象树
    volumeTool = new VolumeTool(this);

}

void MusicPlayer::connectSignalAndSlots()
{
    // 关联BtForm的信号和处理该信号的槽函数
    connect(ui->rec, &BtForm::btClick, this, &MusicPlayer::onBtFormClick);
    connect(ui->radio, &BtForm::btClick, this, &MusicPlayer::onBtFormClick);
    connect(ui->musicHall, &BtForm::btClick, this, &MusicPlayer::onBtFormClick);
    connect(ui->like, &BtForm::btClick, this, &MusicPlayer::onBtFormClick);
    connect(ui->local, &BtForm::btClick, this, &MusicPlayer::onBtFormClick);
    connect(ui->recent, &BtForm::btClick, this, &MusicPlayer::onBtFormClick);

}

// void MusicPlayer::test()
// {

//     ui->lineEdit->setStyleSheet(
//         "QLineEdit {"
//         "    background-color: #E3E3E3;"
//         "    border: 2px solid #ccc;"
//         "    border-radius: 27px;"
//         "    padding: 8px 17px;"      // 上下左右内边距
//         "    font-size: 14px;"
//         "    color: #333;"
//         "}"
//         "QLineEdit:focus {"
//         "    border: 2px solid #0078d4;"
//         "    background-color: #F0F0F0;"
//         "}"
//     );

// }

void MusicPlayer::on_quit_clicked()
{
    this->close();
}

void MusicPlayer::onBtFormClick(int pageId)
{
    // 清除之前BtForm按钮的颜色背景

    // 1.获取所有的BtForm按钮
    QList<BtForm*> btFormList = this->findChildren<BtForm*>();
    for(auto btForm : btFormList)
    {
        if(btForm->getPageId() != pageId)
        {
            btForm->clearBackground();
        }
    }

    //切换页面
    ui->stackedWidget->setCurrentIndex(pageId);
    qDebug()<<"切换页面"<<pageId;

    isDrag = false;
}




void MusicPlayer::on_volume_clicked()
{
    // 1. 获取ui->volume控件的left-top坐标，并转换为基于屏幕的全局坐标
    QPoint point = ui->volume->mapToGlobal(QPoint(0, 0));

    // 2. 计算volumeTool需要移动到的位置：即ui->volume的正上方偏左一半
    QPoint volumeLeftTop = point - QPoint(volumeTool->width()/2, volumeTool->height());

    volumeLeftTop.setX(volumeLeftTop.x()+15);
    volumeLeftTop.setY(volumeLeftTop.y()+30);

    // 3. 移动volumeTool
    volumeTool->move(volumeLeftTop);

    // 4. 窗口显示
    volumeTool->show();
}

